package com.example.uniapp.utils;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.uniapp.R;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 图片管理，保存与加载
 */
public class FileImgUtils {

    private static final ExecutorService executorService = Executors.newSingleThreadExecutor();

    public static Future<Void> saveBitmapAsync(final Bitmap bitmap, final String path) {
        return executorService.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                saveImageBitmapToFileImg(bitmap, path);
                return null;
            }
        });
    }

    /**
     * 将Bitmap转换成文件
     * @param bitmap
     * @param path
     */
    public static void saveImageBitmapToFileImg(Bitmap bitmap, String path) {
        //保存
        File file = new File(path);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void saveImageUriToFileImg(Uri uri, Context context, String path) {
        CustomTarget<Bitmap> target = new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                //保存
                File file = new File(path);
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    resource.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        };

        Glide.with(context)
                .asBitmap()
                .load(uri)
                .into(target);
    }

    /**
     * 获取文件名
     */
    public static String getImgName() {
        String pigName = "/"+ UUID.randomUUID().toString().replace("-", "")+".png";
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()+pigName;
    }

    /**
     * 将系统的图片的路径存入系统
     * @param context
     * @param id
     * @param path
     */
    public static void saveSystemImgtoPath(Context context, int id, String path) {
        Drawable defaultDrawable = ContextCompat.getDrawable(context, id);
        Bitmap defaultBitmap = ((BitmapDrawable) defaultDrawable).getBitmap();
        FileImgUtils.saveBitmapAsync(defaultBitmap, path); //保存图片
    }
}
